var express = require('express');
var md5 = require('./../tools/md5.js')
var AES =require('./../tools/AES.js')
var router = express.Router();

var db = require('../sqldb/index.js');
var nurse =require('../model/nurse.js')
var consultinRoom = require('./../model/consultinRoom.js')
var department = require('./../model/department.js')
var doctor = require('./../model/doctor.js')

/* GET users listing. */

router.post('/getnurse', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return nurse(db.sequelize).findOne({
			where:{
				nUserNum:AES.aesOpen(req.cookies.nUserNum)
			}
		},{
			transaction:t
		}).then(function(data){
			if (data!='') {
				res.json({
					status:1,
					msg:'',
					result:data
				})
			}else{
				res.json({
					status:0,
					msg:'',
					result:''
				})
			}
			
		}).catch(function(err){
			console.log("发生错误：" + err);
		});
	})
});
router.post('/getdept', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return department(db.sequelize).findOne({
			where:{
				dName:AES.aesOpen(req.cookies.dName)
			}
		},{
			transaction:t
		}).then(function(dataTwo){
			if (dataTwo!='') {
				res.json({
					status:1,
					msg:'',
					result:dataTwo
				})
			}else{
				res.json({
					status:0,
					msg:'',
					result:''
				})
			}
		}).catch(function(err){
			console.log("发生错误：" + err);
		});
	})
});


router.post('/getroom', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return consultinRoom(db.sequelize).findAll({
			where:{
				cDeptBelong:AES.aesOpen(req.cookies.dName)
			},
			attributes: ['cName']
		},{
			transaction:t
		}).then(function(data){
			res.json({
				status:1,
				msg:'',
				result:data
			})
		}).catch(function(err){
			console.log("发生错误：" + err);
		});
	})
});

router.post('/addroom', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return consultinRoom(db.sequelize).create({
			cDeptBelong:req.body.cDeptBelong,
			cName:req.body.cName,
		},{
			transaction:t
		}).then(function(data){
			res.json({
				status:1,
				msg:'',
				result:data
			})
		}).catch(function(err){
			console.log("发生错误：" + err);
		});
	})
});

router.post('/closeroom', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return consultinRoom(db.sequelize).destroy({
			where:{
				cDeptBelong:req.body.cDeptBelong,
				cName:req.body.cName,
			}
		},{
			transaction:t
		}).then(function(data){
			res.json({
				status:1,
				msg:'',
				result:data
			})
		}).catch(function(err){
			console.log("发生错误：" + err);
		});
	})
});

router.post('/changname', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return department(db.sequelize).update({
			dName:req.body.newName
		},{
			where:{
				dName: AES.aesOpen(req.cookies.dName)
			}
		}).then(function(data){
			res.cookie('dName',req.body.newName, { 
				expires: new Date(Date.now() + 10000 * 60 * 60 * 24 * 0.25)
			})
			return nurse(db.sequelize).update({
				nDeptBelong:req.body.newName
			},{
				where:{
					nUserNum:AES.aesOpen(req.cookies.nUserNum)
				}
			}).then(function(data){
				return consultinRoom(db.sequelize).update({
					cDeptBelong:req.body.newName
				},{
					where:{
						cDeptBelong: AES.aesOpen(req.cookies.dName)
					}
				}).then(function(data){
					return doctor(db.sequelize).update({
						docDeptBelong:req.body.newName
					},{
						where:{
							docDeptBelong: AES.aesOpen(req.cookies.dName)
						}
					}).then(function(data){
						res.json({
							status:1,
							msg:'',
							result:data
						})
					}).catch(function(err){
						res.json({
							status:0,
							msg:err,
							result:''
						})
						console.log("发生错误：" + err);
					})
				}).catch(function(err){
					res.json({
						status:0,
						msg:err,
						result:''
					})
					console.log("发生错误：" + err);
				})
			}).catch(function(err){
				res.json({
					status:0,
					msg:err,
					result:''
				})
				console.log("发生错误：" + err);
			})
		}).catch(function(err){
			res.json({
				status:0,
				msg:err,
				result:''
			})
			console.log("发生错误：" + err);
		});
	})
});
			
router.post('/changintroduction', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return department(db.sequelize).update({
			deIntroduction:req.body.newIntroduction
		},{
			where:{
				dName: AES.aesOpen(req.cookies.dName)
			}
		}).then(function(data){
			
		}).catch(function(err){
			res.json({
				status:0,
				msg:err,
				result:''
			})
			console.log("发生错误：" + err);
		});
	})
});
router.post('/changis', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return department(db.sequelize).update({
			ifNeed:req.body.ifNeed
		},{
			where:{
				dName: AES.aesOpen(req.cookies.dName)
			}
		}).then(function(data){
			
		}).catch(function(err){
			res.json({
				status:0,
				msg:err,
				result:''
			})
			console.log("发生错误：" + err);
		});
	})
});

router.post('/getcroom', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return consultinRoom(db.sequelize).findAll({
			where:{
				cDeptBelong:AES.aesOpen(req.cookies.dName)
			}
		},{
			transaction:t
		}).then(function(data){
			res.json({
				status:1,
				msg:'',
				result:data
			})
		}).catch(function(err){
			console.log("发生错误：" + err);
		});
	})
});

router.post('/changRoomIntro', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return consultinRoom(db.sequelize).update({
			conIntroduction:req.body.newText
		},{
			where:{
				cName: req.body.cName,
				cDeptBelong:AES.aesOpen(req.cookies.dName)
			}
		}).then(function(data){
			
		}).catch(function(err){
			res.json({
				status:0,
				msg:err,
				result:''
			})
			console.log("发生错误：" + err);
		});
	})
});

router.post('/changDocName', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return consultinRoom(db.sequelize).update({
			docName:req.body.newText
		},{
			where:{
				cName: req.body.cName,
				cDeptBelong:AES.aesOpen(req.cookies.dName)
			}
		}).then(function(data){
			
		}).catch(function(err){
			res.json({
				status:0,
				msg:err,
				result:''
			})
			console.log("发生错误：" + err);
		});
	})
});

router.post('/changeDocIntro', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return consultinRoom(db.sequelize).update({
			docIntroduction:req.body.newText
		},{
			where:{
				cName: req.body.cName,
				cDeptBelong:AES.aesOpen(req.cookies.dName)
			}
		}).then(function(data){
			
		}).catch(function(err){
			res.json({
				status:0,
				msg:err,
				result:''
			})
			console.log("发生错误：" + err);
		});
	})
});
module.exports = router;
